package com.ruoyi.hospital.mongoRepository;

import com.ruoyi.hospital.domain.vo.ConsulationRecordVo;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.web.PageableDefault;

import java.util.List;

public interface ConsulationMongoRepository extends MongoRepository<ConsulationRecordVo,String> {


    /**
    * 自定义查询
    * */
/*    @Query(value = "{ 'receiverId': ?0,  'senderId': ?1,'messageId': { $lt: ?2 } }", count = false)
    public List<ConsulationRecordVo> findByConNoAndTimestamp(String receiverId,String senderId,String messageId,
                                                             @PageableDefault(size = 20, sort = "messageId", direction = Sort.Direction.DESC) Pageable pageable
                                                             );*/

    /*
    * 查询聊天记录
    * */
    @Query(value = "{$or: [" +
            "{ 'receiverId': ?0, 'senderId': ?1, 'messageId': { $lt: ?2 } }, " +
            "{ 'receiverId': ?1, 'senderId': ?0, 'messageId': { $lt: ?2 } }" +
            "]}", count = true)
    public List<ConsulationRecordVo> findByConNoAndTimestamp(
            String receiverId,
            String senderId,
            String messageId,
            @PageableDefault(size = 20, sort = "messageId", direction = Sort.Direction.DESC) Pageable pageable
    );


    /*
    * 查询聊天记录：messageId 为空
    * */
    @Query(value = "{$or: [" +
            "{ 'receiverId': ?0, 'senderId': ?1 }" +
            "{ 'receiverId': ?1, 'senderId': ?0 }" +
            "]}", count = true)
    public List<ConsulationRecordVo> findByConNoAndTimestampDesc(
            String receiverId,
            String senderId,
            @PageableDefault(size = 20, sort = "timestamp", direction = Sort.Direction.DESC) Pageable pageable
    );

    /*
    * 查询传入  医生或者患者的id  查询最新的一条，  同时获得相应的基本信息
    * */
    @Query(value = "{$or: [" +
            "{ 'receiverId': ?0}" +
            "{ 'receiverId': ?0 }" +
            "]}", count = false)
    public List<ConsulationRecordVo> findByUserId(
            String receiverId,
            @PageableDefault(size = 1, sort = "timestamp", direction = Sort.Direction.DESC) Pageable pageable
    );

}
